package com.ncloud.workflow.service.impl;

import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.workflow.service.TaskService;

@Service
public class TaskServiceImpl implements TaskService {

	@Autowired
	private org.activiti.engine.TaskService taskService;
	
	@Override
	public Object getTasks(JSONObject json) {
		if(json.containsKey("taskId")){
			String taskId = json.getString("taskId");
			Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
			return task;
		}
		
		return HttpStatus.NOT_FOUND;
	}

	@Override
	public Object activeTask(JSONObject json) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object createTaskVariable(JSONObject json) {
		if(!json.containsKey("taskId")){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		String taskId = json.getString("taskId");
		String scope = json.containsKey("scope")?json.getString("scope"):"";
		if(json.containsKey("variable")){
			JSONObject variableJson = json.getJSONObject("variable");
			if(StringUtils.isNotBlank(scope) && "local".equals(json.getString("scope"))){
				taskService.setVariablesLocal(taskId, variableJson);
			}else {
				taskService.setVariables(taskId, variableJson);
			}
		}
		return HttpStatus.OK;
	}
	
}
